package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j//生成日志记录对象
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /*查询部门数据
    * */
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list(){
        log.info("查询全部的部门数据");
        //调用serice查询部门数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }


    /*删除部门数据
    {id}代表的是一个路径变量
    * */
    @Log
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据ID删除部门"+id);
        deptService.delete(id);
        return Result.success();
    }

    /*新增部门根据json对象
    * */
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("根据json对象新增部门"+dept);
        deptService.add(dept);
        return Result.success();
    }

    /*根据id查询部门信息
    * */
    @GetMapping("{id}")
    public Result select(@PathVariable Integer id){
        log.info("根据id查询员工信息");
        Dept dept = deptService.select(id);
        return Result.success(dept);
    }

    /*修改部门根据json对象
    * */
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("根据json对象更新部门"+dept);
        deptService.update(dept);
        return Result.success();
    }
}
